package com.perfma.xtest.tool.filejava;

import com.google.common.collect.Maps;
import org.apache.commons.compress.utils.Lists;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: junhui.si
 * @date: 2024-02-23 15:47
 **/
public class LogFile {
    public static void main(String[] args) throws IOException {
        String filePath = "/Users/sijunhui/Downloads/log/slow.txt";
        String pattern = "slow sql (\\d+) millis";
        Pattern regex = Pattern.compile(pattern);
        List<Integer> list = Lists.newArrayList();
        Map<Integer, String> map = Maps.newHashMap();

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                Matcher matcher = regex.matcher(line);
                if (matcher.find()) {
                    String numberStr = matcher.group(1);
                    int number = Integer.parseInt(numberStr);
                    map.put(number, line.replace("\\n", "\n"));
                    list.add(number);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        Collections.sort(list, Collections.reverseOrder());

        for (Integer integer : list) {
            String str = map.get(integer);
            if (integer > 10000
                    && !str.contains("max(id)")
                    && !str.contains("where 1=1\n" +
                    "         \n" +
                    "            and workspace_id = ?")
//                    && !str.contains("0,20")
//                    && !str.contains("response_error_file_key is")
//                    && !str.contains("create_time between")
            ) {
                System.out.println(integer);
                System.out.println(str);
                System.out.println();
                System.out.println();
            }
        }
    }
}
